package com.supermarket.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import ch.qos.logback.core.net.LoginAuthenticator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supermarket.business.domain.SupermarketAdmin;
import com.supermarket.business.domain.SupermarketCashier;
import com.supermarket.business.domain.SupermarketWarehouseManager;
import com.supermarket.business.mapper.SupermarketCashierMapper;
import com.supermarket.business.mapper.SupermarketWarehouseManagerMapper;
import com.supermarket.business.vo.SupermarketAdminVO;
import com.supermarket.common.core.domain.AjaxResult;
import com.supermarket.common.core.domain.Result;
import com.supermarket.common.core.domain.entity.SysUser;
import com.supermarket.common.core.domain.model.LoginBody;
import com.supermarket.common.core.redis.RedisCache;
import com.supermarket.common.utils.DateUtils;
import com.supermarket.common.utils.SecurityUtils;
import com.supermarket.system.mapper.SysUserMapper;
import com.supermarket.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supermarket.business.mapper.SupermarketAdminMapper;
import com.supermarket.business.service.ISupermarketAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 系统管理员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class SupermarketAdminServiceImpl extends ServiceImpl<SupermarketAdminMapper, SupermarketAdmin> implements ISupermarketAdminService
{
    @Resource
    private SupermarketAdminMapper supermarketAdminMapper;
    @Resource
    private SupermarketCashierMapper supermarketCashierMapper;
    @Resource
    private SupermarketWarehouseManagerMapper supermarketWarehouseManagerMapper;
    @Resource
    private RedisCache redisCache;
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 查询系统管理员
     * 
     * @param adminId 系统管理员主键
     * @return 系统管理员
     */
    @Override
    public SupermarketAdmin selectSysAdminByAdminId(Long adminId)
    {
        return supermarketAdminMapper.selectSysAdminByAdminId(adminId);
    }

    /**
     * 查询系统管理员列表
     * 
     * @param supermarketAdmin 系统管理员
     * @return 系统管理员
     */
    @Override
    public List<SupermarketAdmin> selectSysAdminList(SupermarketAdmin supermarketAdmin)
    {
        return supermarketAdminMapper.selectSysAdminList(supermarketAdmin);
    }

    /**
     * 新增系统管理员
     * 
     * @param supermarketAdmin 系统管理员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysAdmin(SupermarketAdmin supermarketAdmin)
    {
        //校验账号id是否合理
        if(Objects.nonNull(supermarketAdmin.getSysId())){
            SysUser user = sysUserMapper.getUserById(supermarketAdmin.getSysId());
            if(Objects.nonNull(user.getBeUsed())&&!Objects.equals(user.getBeUsed(),supermarketAdmin.getAdminId())){
                throw new IllegalArgumentException("该账号已被使用，请更换账号！");
            }
            sysUserMapper.updateBeUsed(supermarketAdmin.getAdminId(), user.getUserId());
        }
        return supermarketAdminMapper.insertSysAdmin(supermarketAdmin);
    }

    /**
     * 修改系统管理员
     * 
     * @param supermarketAdmin 系统管理员
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysAdmin(SupermarketAdmin supermarketAdmin)
    {
        //将之前的账号id解锁
        SupermarketAdmin supermarketAdmin1 = supermarketAdminMapper.selectSysAdminByAdminId(supermarketAdmin.getAdminId());
        SysUser user1 = sysUserMapper.getUserById(supermarketAdmin1.getSysId());
        if(Objects.nonNull(user1)){
            sysUserMapper.updateBeUsed(null, user1.getUserId());
        }

        //校验账号id是否合理
        if(Objects.nonNull(supermarketAdmin.getSysId())){
            SysUser user = sysUserMapper.getUserById(supermarketAdmin.getSysId());
            if(Objects.nonNull(user.getBeUsed())&&!Objects.equals(user.getBeUsed(),supermarketAdmin.getAdminId())){
                throw new IllegalArgumentException("该账号已被使用，请更换账号！");
            }
            sysUserMapper.updateBeUsed(supermarketAdmin.getAdminId(), user.getUserId());
        }

        return supermarketAdminMapper.updateSysAdmin(supermarketAdmin);
    }

    /**
     * 批量删除系统管理员
     * 
     * @param adminIds 需要删除的系统管理员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysAdminByAdminIds(Long[] adminIds)
    {
        for (Long adminId : adminIds) {
            updateSysId(adminId);
        }
        return supermarketAdminMapper.deleteSysAdminByAdminIds(adminIds);
    }

    /**
     * 删除系统管理员信息
     * 
     * @param adminId 系统管理员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysAdminByAdminId(Long adminId)
    {
        updateSysId(adminId);
        return supermarketAdminMapper.deleteSysAdminByAdminId(adminId);
    }

    /**
     * 获取系统管理员VO列表
     * @param supermarketAdmin
     * @return
     */
    @Override
    public List<SupermarketAdminVO> selectSysAdminListVO(SupermarketAdmin supermarketAdmin) {
        List<SupermarketAdmin> list = supermarketAdminMapper.selectSysAdminList(supermarketAdmin);
        List<SupermarketAdminVO> voList = new ArrayList<SupermarketAdminVO>();
        for (SupermarketAdmin admin : list) {
            SupermarketAdminVO vo = new SupermarketAdminVO();
            BeanUtils.copyProperties(admin, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public AjaxResult selectSysAdminVOByAdminId(Long adminId) {
        SupermarketAdmin admin = supermarketAdminMapper.selectSysAdminByAdminId(adminId);
        if(admin != null){
            return AjaxResult.success(getVo(admin));
        }
        return AjaxResult.error("未找到该管理员信息");
    }

    private SupermarketAdminVO getVo(SupermarketAdmin admin){
        SupermarketAdminVO vo = new SupermarketAdminVO();
        BeanUtils.copyProperties(admin, vo);
        return vo;
    }
    /**
     * 释放账号id
     */
    private void updateSysId(Long adminId){
        SupermarketAdmin supermarketAdmin = supermarketAdminMapper.selectSysAdminByAdminId(adminId);
        if(Objects.nonNull(supermarketAdmin.getSysId())){
            sysUserMapper.updateBeUsed(null,supermarketAdmin.getSysId());
        }
    }
}
